<?php
/**
 * Created by PhpStorm.
 * User: luojinyi
 * Date: 2017/11/2
 * Time: 下午9:18
 */

if (!function_exists('config_path')) {
    /**
     * Get the configuration path.
     *
     * @param  string $path
     * @return string
     */
    function config_path($path = '')
    {
        return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
    }
}

if (!function_exists('dingo_route')) {
    /**
     * 根据别名获得url.
     *
     * @param string $version
     * @param string $name
     * @param string $params
     *
     * @return string
     */
    function dingo_route($version, $name, $params = [])
    {
        return app('Dingo\Api\Routing\UrlGenerator')
            ->version($version)
            ->route($name, $params);
    }
}

if (!function_exists('read_file')) {
    /**
     * 读取文件内容
     * @param $fname
     * @return string
     */
    function read_file($fname)
    {
        $content = "";
        if (!file_exists($fname)) {
            echo "The file $fname does not exist\n";
            exit (0);
        }
        $handle = fopen($fname, "rb");
        while (!feof($handle)) {
            $content .= fread($handle, 10000);
        }
        fclose($handle);
        return $content;
    }
}

if (!function_exists('trans')) {
    /**
     * Translate the given message.
     *
     * @param string $id
     * @param array $parameters
     * @param string $domain
     * @param string $locale
     *
     * @return string
     */
    function trans($id = null, $parameters = [], $domain = 'messages', $locale = null)
    {
        if (is_null($id)) {
            return app('translator');
        }

        return app('translator')->trans($id, $parameters, $domain, $locale);
    }
}

if (!function_exists('arrayToObject')) {
    /**
     * 数组转对象
     * @param $e
     * @return object|void
     */
    function arrayToObject($e)
    {
        if (gettype($e) != 'array') return;
        foreach ($e as $k => $v) {
            if (gettype($v) == 'array' || getType($v) == 'object')
                $e[$k] = (object)arrayToObject($v);
        }
        return (object)$e;
    }
}

if (!function_exists('utc_date')) {
    /**
     * UTC时间转换为当前标准时间
     * @param $format
     * @return false|string
     */
    function utc_date($format)
    {
        if(!$format){
            return false;
        }
        date_default_timezone_set('PRC');
        return date("Y-m-d H:i:s", strtotime($format));
    }
}

if(!function_exists('is_phone_number')){
    /**
     * 判断是否是中国手机号码
     * @param $phone
     * @return bool
     */
    function is_phone_number($phone){
        return preg_match('/^1([0-9]{9})/',$phone) == false ? false : true;
    }
}


if(!function_exists('getArrayParam')){
    /**
     * 将参数转化为数组
     * @param $ids
     * @return array
     */
    function getArrayParam($ids){
        return is_array($ids) ? $ids : [$ids];
    }
}


if(!function_exists('firstCharter')){

    /**
     * 首写大写字母
     * @param $str
     * @return null|string
     */
    function firstCharter($str)
    {
        $fchar = ord($str{0});
        if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($str{0});
        $s1 = $str;
        $s2 = $s1;
        //$s1 = iconv("UTF-8","gb2312", $s0);
        //$s2 = iconv("gb2312","UTF-8", $s1);
        if($s2 == $str){$s = $s1;}else{$s = $str;}
        $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
        if($asc >= -20319 and $asc <= -20284) return "A";
        if($asc >= -20283 and $asc <= -19776) return "B";
        if($asc >= -19775 and $asc <= -19219) return "C";
        if($asc >= -19218 and $asc <= -18711) return "D";
        if($asc >= -18710 and $asc <= -18527) return "E";
        if($asc >= -18526 and $asc <= -18240) return "F";
        if($asc >= -18239 and $asc <= -17923) return "G";
        if($asc >= -17922 and $asc <= -17418) return "I";
        if($asc >= -17417 and $asc <= -16475) return "J";
        if($asc >= -16474 and $asc <= -16213) return "K";
        if($asc >= -16212 and $asc <= -15641) return "L";
        if($asc >= -15640 and $asc <= -15166) return "M";
        if($asc >= -15165 and $asc <= -14923) return "N";
        if($asc >= -14922 and $asc <= -14915) return "O";
        if($asc >= -14914 and $asc <= -14631) return "P";
        if($asc >= -14630 and $asc <= -14150) return "Q";
        if($asc >= -14149 and $asc <= -14091) return "R";
        if($asc >= -14090 and $asc <= -13319) return "S";
        if($asc >= -13318 and $asc <= -12839) return "T";
        if($asc >= -12838 and $asc <= -12557) return "W";
        if($asc >= -12556 and $asc <= -11848) return "X";
        if($asc >= -11847 and $asc <= -11056) return "Y";
        if($asc >= -11055 and $asc <= -10247) return "Z";
        return null;
    }
}

if(!function_exists('getClientIp')){

    /**
     * 获取用户真实IP
     * @return mixed|null
     */
    function getClientIp(){
        $ip = null;
        $headers = array('HTTP_X_REAL_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR');
        foreach ($headers as $h){
            $ip = data_get($_SERVER, $h);

            // 有些ip可能隐匿，即为unknown
            if ( isset($ip) && strcasecmp($ip, 'unknown') ){
                break;
            }
        }
        if( $ip ){

            // 可能通过多个代理，其中第一个为真实ip地址
            list($ip) = explode(', ', $ip, 2);
        }

        return $ip;
    }
}

if(!function_exists('getClientCity')){

    /**
     * 获取登录地址
     * @param string $ip
     * @return array|bool
     */
    function getClientCity( $ip = ''){
        $ip = $ip ?: getClientIp();

        //地址
        try{
            $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
            $ip = json_decode(file_get_contents($url));

            return (string)$ip->code == '1' ? false : (array)$ip->data;

        }catch (\Exception $exception){
            return false;
        }
    }
}

